package com.yuyou.activity.comm.api.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

import org.hibernate.validator.constraints.NotBlank;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.yuyou.activity.comm.service.service.ICaptchaService;
import com.yuyou.activity.comm.service.service.ICommSmsVerCodeService;
import com.yuyou.activity.util.bean.ResBoolSimpleInfo;
import com.yuyou.activity.util.bean.ReturnResult;
import com.yuyou.activity.util.enums.CommSmsVerCodeEnums;

@Api(value="SmsController",description="短信接口")
@Validated
@RestController
@RequestMapping(value="/api/sms")
public class SmsController {

	
	@Autowired
	private ICaptchaService captchaService;
	
    @Autowired
    private ICommSmsVerCodeService commSmsVerCodeService;
	

	/**
	 * @param phone
	 * @param captcha
	 * @param token
	 * @return
	 */
    @ApiOperation("发送手机验证码")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "phone", value = "手机号", paramType="query", required = true),
            @ApiImplicitParam(name = "captcha", value = "图形验证码", paramType="query",required = true),
            @ApiImplicitParam(name = "token", value = "图形验证码token", paramType="query", required = true)}
    )
    @RequestMapping(value="/sendSms",method=RequestMethod.POST)
    public ReturnResult<ResBoolSimpleInfo<String>> sendSms(
            @NotBlank(message="phone不能为空")String phone,
            @NotBlank (message="captcha不能为空")String captcha,
            @NotBlank (message="token不能为空")String token){

        //图形验证码
        ResBoolSimpleInfo<String> captchaRes=captchaService.verifyCode(token, captcha);
        if(!captchaRes.isSuccess()){
            return ReturnResult.fail(captchaRes.getMessage());
        }

        ResBoolSimpleInfo<String> stringResBoolSimpleInfo = commSmsVerCodeService.addSmsVerCode(phone, CommSmsVerCodeEnums.type.regisAndReset.getValue());
        
        
        if(!stringResBoolSimpleInfo.isSuccess()){
            return  ReturnResult.fail(stringResBoolSimpleInfo.getMessage());
        }
        return ReturnResult.success(stringResBoolSimpleInfo.getMessage());
    }


	
}
